package com.toocms.jinjuhang.ui.mine.order.logistics_details

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.order_info.KDWLCXData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.ItemBinding

class LogisticsDetailsModel(application: Application, val bundle: Bundle?) :
    BaseViewModel<BaseModel>(application) {

    val logisticsNumber by lazy {
        bundle?.getString(Constants.BundleKey.KEY_LOGISTICS_NUMBER, "") ?: ""
    }


    val items = ObservableArrayList<MultiItemViewModel<LogisticsDetailsModel>>()
    val itemBinding =
        ItemBinding.of<MultiItemViewModel<LogisticsDetailsModel>> { itemBinding, _, item ->
            when (item.itemType) {
                Constants.RecyclerViewItemType.ITEM_TYPE_ZERO -> itemBinding.set(
                    BR.logisticsDetailsInfoItemModel, R.layout.item_logistics_details_info
                )
                Constants.RecyclerViewItemType.ITEM_TYPE_ONE -> itemBinding.set(
                    BR.logisticsDetailsRecordItemModel, R.layout.item_logistics_details_record
                )
            }
        }

    init {
        items += LogisticsDetailsInfoItemModel(
            this@LogisticsDetailsModel,
            ObservableField(""),
            ObservableField(logisticsNumber)
        )
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["logistics_number"] = logisticsNumber
        requestData(Urls.OrderInfo.KDWLCX,
            params,
            resultClass = KDWLCXData::class.java,
            isShowLoading = true,
            result = {
                it.result?.apply {
                    items.clear()
                    items += LogisticsDetailsInfoItemModel(
                        this@LogisticsDetailsModel,
                        ObservableField(expName),
                        ObservableField(logisticsNumber)
                    )
                    list?.forEachIndexed { index, itemData ->
                        items += LogisticsDetailsRecordItemModel(
                            this@LogisticsDetailsModel,
                            ObservableField(itemData),
                            ObservableBoolean(0 == index)
                        )
                    }
                }
            })
    }

}